<template>
    <div>
        <!-- <IM></IM> -->
        dada
        <h1>{{ ip }}</h1>
    </div>
</template>

<script>

import Stomp from 'stompjs'
import os from 'os'

// const MQTT_SERVICE = 'ws://localhost:15674/ws' // mqtt服务地址
// const MQTT_USERNAME = 'guest' // mqtt连接用户名
// const MQTT_PASSWORD = 'guest' // mqtt连接密码
// const MQTT_host = 'guest' // mqtt交换机
// const MQTT_topic = '/exchange/PC_MESSAGE_LIINZHI/PC' // 订阅频道

export default {
    data() {
        return {
            client: null,
            ip: ''
            // title: '测试信息',
            // client: Stomp.client(MQTT_SERVICE),
            // listData: [
            // ]
        }
    },
    computed: {
        // optionSetting() {
        //     return {
        //         step: 1, // 速度，值越大，速度越快
        //         hoverStop: true // 鼠标悬停效果，false为关闭该效果
        //         // 　　　　　　　　　　singleHeight: 120,//单行停顿
        //         // 　　　　　　　　　　waitTime: 2500,//单行停顿的时间
        //     }
        // }
    },
    created() {
        this.connect()// stomp连接mq
    },
    mounted() {
        this.getUserIP((ip) => {
            this.ip = ip;
        });
    },
    methods: {
        onConnected: function (frame) {
            // this.client.send("/queue/exchange_pushmsg", { "content-type": "text/plain" }, "11111111");
            // data.body是接收到的数据
            // this.client.subscribe("/queue/exchange_pushmsg", function (data) {
            this.client.subscribe("/exchange/im-end.current/pm1", function (data) {
                debugger;
                let msg = data.body;
                console.log("收到数据：" + msg);
            });
        },
        onFailed: function (error) {
            // connect();
            console.error(new Date() +', error: ' + error);
            window.setTimeout(()=>{
                this.connect();
            },5000);
        },
        connect() {
            this.client = Stomp.client('ws://127.0.0.1:19101/ws');
            this.client.heartbeat.outgoing = 50000; // if 5000 means client will send heart beat every 5000ms
            this.client.heartbeat.incoming = 50000;// if 0 means client does not want to receive heartbeats from server
            // 获得Stomp client对象
            // this.stomp = Stomp.over(ws);
            this.client.connect('imend', '123456', this.onConnected, this.onFailed, '/');
            // console.log(">>>连接上http://127.0.0.1:15674");
        },
        getUserIP(onNewIP) {
            let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
            let pc = new MyPeerConnection({
                iceServers: []
            });
            let noop = () => {
            };
            let localIPs = {};
            let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;
            let iterateIP = (ip) => {
                if (!localIPs[ip]) onNewIP(ip);
                localIPs[ip] = true;
            };
            pc.createDataChannel('');
            pc.createOffer().then((sdp) => {
                sdp.sdp.split('\n').forEach(function (line) {
                    if (line.indexOf('candidate') < 0) return;
                    line.match(ipRegex).forEach(iterateIP);
                });
                pc.setLocalDescription(sdp, noop, noop);
            }).catch((reason) => {
            });
            pc.onicecandidate = (ice) => {
                if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
                ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
            };
        }
    }
}
</script>

<style>
</style>
